package controller

import (
	"context"
	v1 "gf2DemoServer/api/v1"
	"gf2DemoServer/internal/model"
	"gf2DemoServer/internal/service"
)

var (
	User = cUser{}
)

type cUser struct{}

// Login 登录
func (c *cUser) Login(ctx context.Context, req *v1.UserLoginReq) (res *v1.UserLoginRes, err error) {
	userEntity, err := service.User().Login(ctx, &model.UserLoginInput{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		return
	}
	tokenString, err := service.Token().Build(ctx, userEntity)
	if err != nil {
		return
	}
	res = &v1.UserLoginRes{
		Token: tokenString,
	}
	return
}

// Info 用户信息
func (c *cUser) Info(ctx context.Context, req *v1.UserInfoReq) (res *v1.UserInfoRes, err error) {
	myClaims := service.Token().Check(ctx, true)
	userEntity, userErr := service.User().GetInfoByMobile(ctx, myClaims.Mobile)
	if userErr != nil {
		return
	}
	res = &v1.UserInfoRes{
		Id:     userEntity.Id,
		Name:   userEntity.Name,
		Avatar: userEntity.Avatar,
		Money:  userEntity.Money,
		Score:  userEntity.Score,
		Gender: userEntity.Gender,
		Mobile: userEntity.Mobile,
	}
	return
}
